<script>
import axios from 'axios'
import { getProvinceMapInfo } from '@/utils/map_utils'
import { mapState } from 'vuex'

export default {
  name: 'MapComponent',
  data () {
    return {
      chartInstance: null,
      allData: null,
      mapData: {} // 缓存地图数据
    }
  },
  mounted () {
    this.initChart()
    // this.getData()
    // 发送数据给服务器
    this.$socket.send({
      action: 'getData',
      socketType: 'MapData',
      chartName: 'map',
      value: ''
    })

    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  created () {
    this.$socket.registerCallBack('MapData', this.getData)
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    this.$socket.unRegisterCallBack('MapData')
  },
  methods: {
    async initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme)
      // 获取中国地图的矢量数据
      const ret = await axios.get(
        'http://localhost:8899/static/map/china.json'
      )
      this.$echarts.registerMap('china', ret.data)
      const initOption = {
        title: {
          text: '▎ 商家分布',
          left: 20,
          top: 20
        },
        geo: {
          type: 'map',
          map: 'china',
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: '#2E72BF',
            borderColor: '#333'
          }
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('click', async (arg) => {
        const provinceMapInfo = getProvinceMapInfo(arg.name)
        // 获取省份的地图数据
        let ret = null
        if (provinceMapInfo === undefined) return
        if (!this.mapData[provinceMapInfo.key]) {
          ret = await axios.get('http://localhost:8899' + provinceMapInfo.path)
          this.mapData[provinceMapInfo.key] = ret.data
        }
        this.$echarts.registerMap(
          provinceMapInfo.key,
          this.mapData[provinceMapInfo.key]
        )
        // 切换地图
        const changeOption = {
          geo: {
            map: provinceMapInfo.key
          }
        }
        this.chartInstance.setOption(changeOption)
      })
    },
    async getData () {
      // 获取数据
      const { data: ret } = await this.$http.get('map')
      this.allData = ret
      this.updateChart()
    },
    updateChart () {
      // 处理图表需要的数据
      // 图例数据
      const legendArr = this.allData.map((item) => {
        return item.name
      })
      // series数据
      const seriesArr = this.allData.map((item) => {
        // 一个类别的所有散点数据
        return {
          type: 'effectScatter',
          name: item.name,
          data: item.children,
          rippleEffect: {
            scale: 5,
            brushType: 'stroke'
          },
          coordinateSystem: 'geo'
        }
      })
      const dataOption = {
        series: seriesArr,
        legend: {
          data: legendArr
        }
      }
      this.chartInstance.setOption(dataOption)
    },
    screenAdapter () {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGape: titleFontSize / 2,
          textStyle: {
            textStyle: {
              fontSize: titleFontSize / 2
            }
          }
        }
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    revertMap () {
      // 回到中国地图
      const revertOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(revertOption)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      this.chartInstance.dispose() // 销毁当前图表
      this.initChart()
      this.screenAdapter()
      this.updateChart()
    }
  }
}
</script>

<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<style scoped></style>
